package com.ruoyi.options.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.clp.domain.CCfs;
import com.ruoyi.clp.domain.CCompany;
import com.ruoyi.clp.service.CCfsService;
import com.ruoyi.clp.service.CCompanyService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.contract.domain.VCtmsClient;
import com.ruoyi.contract.mapper.VCtmsClientMapper;
import com.ruoyi.contract.service.VCtmsClientService;
import com.ruoyi.options.domain.Option;
import com.ruoyi.options.service.IOptionsService;
import com.ruoyi.tos.domain.VCtmsVesselSchedule;
import com.ruoyi.tos.mapper.VCtmsVesselScheduleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 毕京瑞
 * @createTime 2023-06-20 17:15
 */
@Service
public class IOptionsServiceImpl implements IOptionsService {
	
	@Autowired
	private CCfsService cfsService;
	@Autowired
	private CCompanyService companyService;
	@Autowired
	private VCtmsClientService vCtmsClientService;
	@Autowired
	private VCtmsClientMapper vCtmsClientMapper;
	@Autowired
	private VCtmsVesselScheduleMapper vCtmsVesselScheduleMapper;
	
	
	/**
	 * 通过查询C_COMPANY表，获取COMPANY_COD	CN_COMPANY_NAM两个字段，封装成Option对象，返回List<Option>
	 * 需要distinct处理
	 *
	 * @return
	 */
	@Override
	public List<Option> getCompanyCodeOptions() {
		// 1. 创建queryWrapper
		QueryWrapper<CCompany> queryWrapper = new QueryWrapper<>();
		
		// 2. 设置查询条件
		
		// distinct查询字段
		queryWrapper.select("distinct COMPANY_COD", "CN_COMPANY_NAM");
		// 注意：company 表里有个类似我们del flag的abandoned_mark字段 （废弃标志，0: 正常: 1: 废弃）
		queryWrapper.eq("ABANDONED_MARK", 0);
		
		
		// 3. 查询
		List<CCompany> cCompanies = companyService.list(queryWrapper);
		
		
		// 4. 封装成Option对象,把List<CCompany>转换成List<Option>
		/**
		 * company的COMPANY_COD字段对应Option的key、value
		 * company的CN_COMPANY_NAM字段对应Option的label
		 */
		List<Option> options = new ArrayList<>();
		for (CCompany cCompany : cCompanies) {
			Option option = new Option();
			option.setKey(cCompany.getCompanyCod());
			option.setValue(cCompany.getCompanyCod());
			option.setLabel(cCompany.getCnCompanyNam());
			options.add(option);
		}
		
		
		// 5. 返回
		return options;
		
		
	}
	
	@Override
	public List<Option> getCfsCodeOptions() {
		// 1. 创建queryWrapper
		QueryWrapper<CCfs> queryWrapper = new QueryWrapper<>();
		
		// 2. distinct查询 COMPANY_COD CFS_COD CFS_SHORT
		queryWrapper.select("distinct COMPANY_COD", "CFS_COD", "CFS_SHORT");
		
		// 3. 查询
		List<CCfs> cCfs = cfsService.list(queryWrapper);
		
		// 4. 封装成Option对象,把List<CCompany>转换成List<Option>
		/**
		 * CCfs的COMPANY_COD字段对应Option的key、value
		 * CCfs的CFS_SHORT字段对应Option的label
		 * CCfs的COMPANY_COD字段对应Option的remark
		 */
		List<Option> options = new ArrayList<>();
		for (CCfs cCf : cCfs) {
			Option option = new Option();
			option.setKey(cCf.getCfsCod());
			option.setValue(cCf.getCfsCod());
			option.setLabel(cCf.getCfsShort());
			option.setRemark(cCf.getCompanyCod());
			options.add(option);
		}
		
		// 5. 返回
		return options;
		
	}
	
	@Override
	public List<Option> getCusNameOptions() {
		// 1. 创建queryWrapper
		QueryWrapper<VCtmsClient> queryWrapper = new QueryWrapper<>();
		
		// 2. distinct查询V_CTMS_CLIENT视图的cnCustomerNam，要求abandonedMark（废弃标志）为0（正常）
		queryWrapper.select("distinct CN_CUSTOMER_NAM, CUSTOMER_COD");
		queryWrapper.eq("ABANDONED_MARK", 0);
//		queryWrapper.eq("COMPANY_COD",SecurityUtils.getCompanyCode());

		// 3. 查询
		List<VCtmsClient> vCtmsClients = vCtmsClientService.list(queryWrapper);
		
		// 4. 封装成Option对象,把List<VCtmsClient>转换成List<Option>
		/**
		 * VCtmsClient的cnCustomerNam字段对应Option的key、value
		 * VCtmsClient的cnCustomerNam字段对应Option的label
		 */
		System.out.println(vCtmsClients);
		List<Option> options = new ArrayList<>();
		for (VCtmsClient vCtmsClient : vCtmsClients) {
			Option option = new Option();
			option.setKey(vCtmsClient.getCustomerCod() + vCtmsClient.getCnCustomerNam());
			option.setValue(vCtmsClient.getCnCustomerNam());
			option.setLabel(vCtmsClient.getCustomerCod() + vCtmsClient.getCnCustomerNam());
			options.add(option);
		}
		
		// 5. 返回
		return options;
		
		
	}
	
	@Override
	public List<Option> getShipNameOptions() {
		// 1.创建queryWrapper
		QueryWrapper<VCtmsVesselSchedule> queryWrapper = new QueryWrapper<>();
		// 2. distinct查询V_CTMS_VESSEL_SCHEDULE视图的CN_SHIP_NAM
		queryWrapper.select("distinct CN_SHIP_NAM");
//		queryWrapper.eq("COMPANY_COD",SecurityUtils.getCompanyCode());
		// 3. 查询
		List<VCtmsVesselSchedule> vCtmsVesselSchedules = vCtmsVesselScheduleMapper.selectList(queryWrapper);
		// 4. 封装成Option对象,把List<VCtmsVesselSchedule>转换成List<Option>
		/**
		 * VCtmsVesselSchedule的cnShipNam字段对应Option的key、value
		 * VCtmsVesselSchedule的cnShipNam字段对应Option的label
		 */
		List<Option> options = new ArrayList<>();
		for (VCtmsVesselSchedule vCtmsVesselSchedule : vCtmsVesselSchedules) {
			Option option = new Option();
			option.setKey(vCtmsVesselSchedule.getCnShipNam());
			option.setValue(vCtmsVesselSchedule.getCnShipNam());
			option.setLabel(vCtmsVesselSchedule.getCnShipNam());
			options.add(option);
		}
		
		// 5. 返回
		return options;
		
	}
	
	
	@Override
	public List<Option> getVoyageOptions() {
		
		// 1.创建queryWrapper
		QueryWrapper<VCtmsVesselSchedule> queryWrapper = new QueryWrapper<>();
		
		// 2.执行查询
		
		// 2.1 distinct查询V_CTMS_VESSEL_SCHEDULE视图的IM_VOYAGE 进口航次
		// 执行查询
		queryWrapper.select("distinct IM_VOYAGE");
		//匹配登录用户的公司代码
//		queryWrapper.eq("COMPANY_COD",SecurityUtils.getCompanyCode());
		List<VCtmsVesselSchedule> vCtmsVesselSchedules1 = vCtmsVesselScheduleMapper.selectList(queryWrapper);
		// 进口航次set
		Set<String> imVoyages = new HashSet<>();
		for (VCtmsVesselSchedule vCtmsVesselSchedule : vCtmsVesselSchedules1) {
			imVoyages.add(vCtmsVesselSchedule.getImVoyage());
		}
		// 2.2 distinct查询V_CTMS_VESSEL_SCHEDULE视图的EX_VOYAGE 出口航次
		// 执行查询
		queryWrapper.select("distinct EX_VOYAGE");
		List<VCtmsVesselSchedule> vCtmsVesselSchedules2 = vCtmsVesselScheduleMapper.selectList(queryWrapper);
		// 出口航次set
		Set<String> exVoyages = new HashSet<>();
		for (VCtmsVesselSchedule vCtmsVesselSchedule : vCtmsVesselSchedules2) {
			exVoyages.add(vCtmsVesselSchedule.getExVoyage());
		}
		
		// 3. 转换成List<Option>
		// 出口、进口航次合并
		Set<String> voyages = new HashSet<>();
		voyages.addAll(imVoyages);
		voyages.addAll(exVoyages);
		// 组装List<Option>
		List<Option> options = new ArrayList<>();
		for (String voyage : voyages) {
			Option option = new Option();
			option.setKey(voyage);
			option.setValue(voyage);
			option.setLabel(voyage);
			options.add(option);
		}
		// 4. 返回
		return options;
		
	}
}

	
